package com.hjj.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DB {
    private static Connection cn = null;

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("实例化MySQL驱动失败");
        }
    }

    public static Connection open(){
        // 创建连接协议
        String url = "jdbc:mysql://localhost:3306/studb";
        String username = "root";
        String password = "root";

        try {
            cn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("获取连接对象失败");
        }

        return cn;
    }

    public static void close(){
        try {
            if (cn != null && cn.isClosed()){
                cn.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭连接对象失败");
        }
    }
}
